{*******************************************************************************
* The contents of this file are used with permission, subject to the Mozilla   *
* Public License Version 1.1 (the "License"); you may not use this file except *
* in compliance with the License. You may obtain a copy of the License at      *
* http://www.mozilla.org/MPL/                                                  *
*                                                                              *
* Software distributed under the License is distributed on an "AS IS" basis,   *
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for *
* the specific language governing rights and limitations under the License.    *
*                                                                              *
* The Original Code is distributed as part of the "mediate" product and is     *
* Copyright (C) @home media limited. All Rights Reserved.                      *
*                                                                              *
* Email: support@athomemedia.co.uk                                             *
* Web:   http://www.athomemedia.co.uk                                          *
*******************************************************************************}

unit CaptionedObject;

interface

uses Classes, UIObjects, Captions;

type
  TAHMUICaptionedDisplayObject = class(TAHMUIDisplayObject)
  private
    FCaption: TAHMCaption;
    FCaptionX, FCaptionY: Single;
    FVirtCaptionX: Integer;
    FVirtCaptionY: Integer;
  protected
    procedure SetVirtualCaptionX(X: Integer);
    procedure SetVirtualCaptionY(Y: Integer);
    procedure ClearProperties; override;
  public
    property Caption: TAHMCaption read FCaption write FCaption;
    property CaptionX: Single read FCaptionX write FCaptionX;
    property CaptionY: Single read FCaptionY write FCaptionY;
    property VirtualCaptionX: Integer read FVirtCaptionX write SetVirtualCaptionX;
    property VirtualCaptionY: Integer read FVirtCaptionY write SetVirtualCaptionY;
    procedure Assign(Source: TPersistent); override;
    procedure Rename(Name: String); override;
  end;

implementation

uses SysUtils, Screen;


procedure TAHMUICaptionedDisplayObject.SetVirtualCaptionX(X: Integer);
begin
  FVirtCaptionX := X;
  FCaptionX := ScreenInstance.ScaleVirtualX(X);
end;

procedure TAHMUICaptionedDisplayObject.SetVirtualCaptionY(Y: Integer);
begin
  FVirtCaptionY := Y;
  FCaptionY := ScreenInstance.ScaleVirtualY(Y);
end;

procedure TAHMUICaptionedDisplayObject.ClearProperties;
begin
  inherited;

  // Clear custom captioned object properties
  FCaption := nil;
  VirtualCaptionX := 0;
  VirtualCaptionY := 0;
end;

procedure TAHMUICaptionedDisplayObject.Assign(Source: TPersistent);
begin
  inherited Assign(Source);

  // Copy caption properties from self to destination object
  with Source as TAHMUICaptionedDisplayObject do
  begin
    Self.Caption := Caption;
    Self.VirtualCaptionX := VirtualCaptionX;
    Self.VirtualCaptionY := VirtualCaptionY;
  end;
end;

procedure TAHMUICaptionedDisplayObject.Rename(Name: String);
begin
  inherited;

  // Keep preview text in sync with changes to object name
  PreviewDisplayText := Name;
end;

end.
